<template>
  <ul :class="simpleWrapClasses" :style="styles">
    <li :class="simplePagerClasses" :title="currentPage + '/' + allPages">
      共{{ total }}条，每页&nbsp;
      <Select v-model="currentPageSize" @on-change="onSize" :disabled="disabled">
        <Option v-for="size in pageSizeOpts" :key="'size-' + size" :value="size">{{size}}</Option>
      </Select>&nbsp;条
    </li>
    <li>
      &emsp;&emsp;到第&nbsp;
      <input
        :class="[prefixCls + '--input']"
        type="text"
        size="small"
        :value="currentPage"
        autocomplete="off"
        spellcheck="false"
        :disabled="disabled"
        @keydown="keyDown"
        @keyup="keyUp"
        @change="keyUp"
      />&nbsp;页&nbsp;
    </li>

    <li title="刷新" class="refresh-btn" @click="refresh">
      <a href="javascript:void(0)">
        <i class="iconfont icon-shuaxin"></i>
      </a>
    </li>
    <li title="上一页" :class="prevClasses" @click="prev">
      <a href="javascript:void(0)">
        <i class="ivu-icon ivu-icon-md-arrow-back"></i>
      </a>
    </li>
    <!-- <li :title="currentPage" :class="[prefixCls + '-item',prefixCls + '-item-active']">
      <a href="javascript:void(0)">{{ currentPage }}</a>
    </li>-->
    <li
      title="前五页"
      v-if="currentPage > 5"
      :class="[prefixCls + '-item', prefixCls + '-item-jump-prev']"
      @click="fastPrev"
    >
      <a>
        <i class="ivu-icon ivu-icon-ios-more"></i>
      </a>
    </li>

    <li
      :title="currentPage - 3"
      v-if="currentPage - 3 > 0"
      :class="[prefixCls + '-item']"
      @click="changePage(currentPage - 3)"
    >
      <a href="javascript:void(0)">{{ currentPage - 3 }}</a>
    </li>
    <li
      :title="currentPage - 2"
      v-if="currentPage - 2 > 0"
      :class="[prefixCls + '-item']"
      @click="changePage(currentPage - 2)"
    >
      <a href="javascript:void(0)">{{ currentPage - 2 }}</a>
    </li>
    <li
      :title="currentPage - 1"
      v-if="currentPage - 1 > 0"
      :class="[prefixCls + '-item']"
      @click="changePage(currentPage - 1)"
    >
      <a href="javascript:void(0)">{{ currentPage - 1 }}</a>
    </li>
    <li :title="currentPage" :class="[prefixCls + '-item',prefixCls + '-item-active']">
      <a href="javascript:void(0)">{{ currentPage }}</a>
    </li>
    <li
      :title="currentPage + 1"
      v-if="currentPage + 1 <= allPages"
      :class="[prefixCls + '-item']"
      @click="changePage(currentPage + 1)"
    >
      <a href="javascript:void(0)">{{ currentPage + 1 }}</a>
    </li>
    <li
      :title="currentPage + 2"
      v-if="currentPage + 2 <= allPages"
      :class="[prefixCls + '-item']"
      @click="changePage(currentPage + 2)"
    >
      <a href="javascript:void(0)">{{ currentPage + 2 }}</a>
    </li>
    <li
      :title="currentPage + 3"
      v-if="currentPage + 3 <= allPages"
      :class="[prefixCls + '-item']"
      @click="changePage(currentPage + 3)"
    >
      <a href="javascript:void(0)">{{ currentPage + 3 }}</a>
    </li>

    <li
      title="后五页"
      v-if="allPages - currentPage >= 5"
      :class="[prefixCls + '-item', prefixCls + '-item-jump-next']"
      @click="fastNext"
    >
      <a>
        <i class="ivu-icon ivu-icon-ios-more"></i>
      </a>
    </li>

    <li title="下一页" :class="nextClasses" @click="next">
      <a href="javascript:void(0)">
        <i class="ivu-icon ivu-icon-md-arrow-forward"></i>
      </a>
    </li>
  </ul>
</template>
<script>
import { oneOf } from '@/utils/assist'
// import Options from './options.vue';

const prefixCls = 'i-pager'

export default {
  name: 'Page',
  props: {
    current: {
      type: Number,
      default: 1
    },
    total: {
      type: Number | String,
      default: 0
    },
    pageSize: {
      type: Number,
      default: 10
    },
    pageSizeOpts: {
      type: Array,
      default () {
        return [10, 20, 30, 40]
      }
    },
    placement: {
      validator (value) {
        return oneOf(value, ['top', 'bottom'])
      },
      default: 'bottom'
    },
    transfer: {
      type: Boolean,
      default () {
        return !this.$IVIEW || this.$IVIEW.transfer === '' ? false : this.$IVIEW.transfer
      }
    },
    simple: {
      type: Boolean,
      default: false
    },
    className: {
      type: String
    },
    styles: {
      type: Object
    },
    prevText: {
      type: String,
      default: ''
    },
    nextText: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      prefixCls: prefixCls,
      currentPage: this.current,
      currentPageSize: this.pageSize
    }
  },
  watch: {
    total (val) {
      let maxPage = Math.ceil(parseInt(val) / this.currentPageSize)
      if (maxPage < this.currentPage) {
        this.currentPage = maxPage === 0 ? 1 : maxPage
      }
    },
    current (val) {
      this.currentPage = val
    },
    pageSize (val) {
      this.currentPageSize = val
    }
  },
  computed: {
    isSmall () {
      return !!this.size
    },
    allPages () {
      const allPage = Math.ceil(parseInt(this.total) / this.currentPageSize)
      return allPage === 0 ? 1 : allPage
    },
    simpleWrapClasses () {
      return [
        `${prefixCls}`,
        `${prefixCls}-simple`,
        {
          [`${this.className}`]: !!this.className
        }
      ]
    },
    simplePagerClasses () {
      return `${prefixCls}-simple-pager`
    },
    wrapClasses () {
      return [
        `${prefixCls}`,
        {
          [`${this.className}`]: !!this.className,
          [`${prefixCls}-with-disabled`]: this.disabled,
          mini: !!this.size
        }
      ]
    },
    prevClasses () {
      return [
        `${prefixCls}-prev`,
        {
          [`${prefixCls}-disabled`]: this.currentPage === 1 || this.disabled,
          [`${prefixCls}-custom-text`]: this.prevText !== ''
        }
      ]
    },
    nextClasses () {
      return [
        `${prefixCls}-next`,
        {
          [`${prefixCls}-disabled`]: this.currentPage === this.allPages || this.disabled,
          [`${prefixCls}-custom-text`]: this.nextText !== ''
        }
      ]
    },
    firstPageClasses () {
      return [
        `${prefixCls}-item`,
        {
          [`${prefixCls}-item-active`]: this.currentPage === 1
        }
      ]
    },
    lastPageClasses () {
      return [
        `${prefixCls}-item`,
        {
          [`${prefixCls}-item-active`]: this.currentPage === this.allPages
        }
      ]
    }
  },
  methods: {
    changePage (page) {
      if (this.disabled) return
      if (this.currentPage !== page) {
        this.currentPage = page
        this.$emit('update:current', page)
        this.$emit('on-change', page)
      }
    },
    prev () {
      if (this.disabled) return
      const current = this.currentPage
      if (current <= 1) {
        return false
      }
      this.changePage(current - 1)
    },
    next () {
      if (this.disabled) return
      const current = this.currentPage
      if (current >= this.allPages) {
        return false
      }
      this.changePage(current + 1)
    },
    refresh () {
      this.$emit('update:current', this.currentPage)
      this.$emit('on-change', this.currentPage)
    },
    fastPrev () {
      if (this.disabled) return
      const page = this.currentPage - 5
      if (page > 0) {
        this.changePage(page)
      } else {
        this.changePage(1)
      }
    },
    fastNext () {
      if (this.disabled) return
      const page = this.currentPage + 5
      if (page > this.allPages) {
        this.changePage(this.allPages)
      } else {
        this.changePage(page)
      }
    },
    onSize (pageSize) {
      if (this.disabled) return
      this.currentPageSize = pageSize
      this.$emit('on-page-size-change', pageSize)
      this.changePage(1)
    },
    onPage (page) {
      if (this.disabled) return
      this.changePage(page)
    },
    keyDown (e) {
      const key = e.keyCode
      const condition = (key >= 48 && key <= 57) || (key >= 96 && key <= 105) || key === 8 || key === 37 || key === 39

      if (!condition) {
        e.preventDefault()
      }
    },
    keyUp (e) {
      const key = e.keyCode
      const val = parseInt(e.target.value)

      if (key === 38) {
        this.prev()
      } else if (key === 40) {
        this.next()
      } else if (key === 13) {
        let page = 1

        if (val > this.allPages) {
          page = this.allPages
        } else if (val <= 0 || !val) {
          page = 1
        } else {
          page = val
        }

        e.target.value = page
        this.changePage(page)
      }
    }
  }
}
</script>
<style lang="less" scoped>
.i-pager {
  text-align: right;
  padding: 10px;
  line-height: 30px;
  li {
    display: inline-block;

    a {
      display: block;
    }
  }
}
.i-pager--input {
  width: 30px;
  text-align: center;
  font-size: 14px;
  box-sizing: content-box;
}
.i-pager-next {
  border-left: none;
}
.i-pager-item {
  font-size: 14px;
  border-left: none;
}

.i-pager-prev,
.i-pager-next,
.i-pager-item {
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  box-sizing: content-box;
}
.ivu-select {
  width: 55px;
  text-align: center;
  vertical-align: middle;
}

.refresh-btn{
  padding: 0 10px;
}
</style>
